package com.zxg.util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.taobao.api.internal.util.WebUtils;

public class AIQingYunUtil {

  public static String doAi(String msg) {
    String rtn = null;

    try {
      String encode = URLEncoder.encode(msg, "utf8");
      String url = "http://api.qingyunke.com/api.php?key=free&appid=0&msg=" + encode;

      String res = WebUtils.doGet(url, null);
      if (res != null) {
        JSONObject obj = JSON.parseObject(res);
        if (obj != null) {
          return obj.getString("content");
        }
      }
    } catch (UnsupportedEncodingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    return rtn;
  }

  public static void main(String[] args) {
    System.out.println(doAi("杭州天气").replaceAll("[{]br[}]", "\n"));
  }
}
